package megrez.boot.autoconfigure.minio;

import java.util.List;
import java.util.function.Consumer;

import io.minio.BucketExistsArgs;
import io.minio.GetObjectLockConfigurationArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.messages.Bucket;
import io.minio.messages.ObjectLockConfiguration;
import lombok.RequiredArgsConstructor;

/**
 * MinIO桶操作服务
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@RequiredArgsConstructor
public class MinioBucketClient {
    private final MinioClient client;

    /**
     * 桶是否存在
     * 
     * @param bucketName 桶名
     * @param consumer   builder消费
     * @return
     */
    public boolean bucketExists(String bucketName, Consumer<BucketExistsArgs.Builder> consumer) {
        try {
            BucketExistsArgs.Builder builder = BucketExistsArgs.builder()
                    .bucket(bucketName);

            if (consumer != null) {
                consumer.accept(builder);
            }

            return client.bucketExists(builder.build());
        } catch (Exception e) {
            throw new MinioRuntimeException(e);
        }
    }

    /**
     * 桶是否存在
     * 
     * @param bucketName 桶名
     * @return
     */
    public boolean bucketExists(String bucketName) {
        return bucketExists(bucketName, null);
    }

    /**
     * 所有桶信息
     * 
     * @return
     */
    public List<Bucket> listBuckets() {
        try {
            return client.listBuckets();
        } catch (Exception e) {
            throw new MinioRuntimeException(e);
        }

    }

    /**
     * 创建桶 
     * 
     * @param bucketName 桶名
     * @param consumer builder消费
     */
    public void makeBucket(String bucketName, Consumer<MakeBucketArgs.Builder> consumer) {
        try {
            MakeBucketArgs.Builder builder = MakeBucketArgs.builder()
                    .bucket(bucketName);

            if (consumer != null) {
                consumer.accept(builder);
            }

            client.makeBucket(builder.build());
        } catch (Exception e) {
            throw new MinioRuntimeException(e);
        }

    }

    /**
     * 创建桶 
     * 
     * @param bucketName 桶名
     */
    public void makeBucket(String bucketName) {
        makeBucket(bucketName, null);
    }


    /**
     * 获取存储桶中的对象锁定配置
     * 
     * @param bucketName 桶名
     * @param consumer   builder消费
     * @return
     */
    public ObjectLockConfiguration getObjectLockConfiguration(String bucketName,
            Consumer<GetObjectLockConfigurationArgs.Builder> consumer) {
        try {
            GetObjectLockConfigurationArgs.Builder builder = GetObjectLockConfigurationArgs.builder()
                    .bucket(bucketName);
            if (consumer != null) {
                consumer.accept(builder);
            }
            return client.getObjectLockConfiguration(builder.build());
        } catch (Exception e) {
            throw new MinioRuntimeException(e);
        }
    }

    /**
     * 获取存储桶中的对象锁定配置
     * 
     * @param bucketName 桶名
     * @return
     */
    public ObjectLockConfiguration getObjectLockConfiguration(String bucketName) {
        return getObjectLockConfiguration(bucketName, null);
    }
}
